package com.etop.weibo.dao;

import com.etop.weibo.beans.Say;
import com.etop.weibo.dto.UserSayDTO;
import java.util.List;

import com.etop.weibo.util.WeiBoException;
import com.etop.weibo.util.HibernateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.Session;

public class SayDao extends BaseDao<Say>{

    private final static String ID="id";
    private final static String USER_ID="userId";
    private final static String WHAT="what";
    private final static String PRAISE="praise";
    private final static String CREATE_TIME="createTime";
    
    @Override
    public void add(Say say) {
        super.add(say);
    }

    @Override
    public void update(Say say) {
        super.update(say);
    }

    @Override
    public void delete(int id) {
//        super.delete(Say.class, id);
    }
    public void deleteAll(int userId){
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
//        super.deleteByHQL(Say.class, params);
    }

    @Override
    public Say load(int id) {
//        return super.load(Say.class, id);
        return null;
    }

//    public List<UserSayDTO> list() {
//        Session session = HibernateUtil.openSession();
//        List<Object[]> userSays = session.createSQLQuery("SELECT u.nickname, s.id, s.u_id, s.what, s.praise, s.create_time"
//                + " FROM t_say s LEFT JOIN t_user u ON s.u_id=u.id ORDER BY s.create_time DESC")
//                .list();
//        List<UserSayDTO> list = new ArrayList<>();
//        for (Object[] o : userSays) {
//            UserSayDTO us = new UserSayDTO((String) o[0], (int) o[1], (int) o[2], (String) o[3], (int) o[4], (Date) o[5]);
//            list.add(us);
//        }
//        return list;
//    }

    public List<UserSayDTO> list(int userId) {
        Session session = HibernateUtil.openSession();
        List<Object[]> userSays = session.createSQLQuery("SELECT u.nickname as nickname, s.id as sayId, s.u_id as userId, s.what as what, s.praise as praise "
                + "FROM t_say s LEFT JOIN t_user u WHERE s.u_id=u.id and u_id=? ORDER BY s.createTime DESC")
                .setInteger(0, userId)
                .list();
        List<UserSayDTO> list = new ArrayList<UserSayDTO>();
        for (Object[] o : userSays) {
            UserSayDTO us = new UserSayDTO((String) o[0], (int) o[1], (int) o[2], (String) o[3], (int) o[4], (Date) o[5]);
            list.add(us);
        }
        return list;
    }

    public void praise(int id) {
        Session session = null;
        try {
            session = HibernateUtil.openSession();
            session.beginTransaction();
            session.createQuery("update Say set praise=praise+1 where id=:id").setInteger("id", id).executeUpdate();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session != null) {
                session.getTransaction().rollback();
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    public void unPraise(int id) {
        Session session = null;
        try {
            session = HibernateUtil.openSession();
            session.beginTransaction();
            session.createQuery("update Say set praise=praise-1 where id=:id").setInteger("id", id).executeUpdate();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session != null) {
                session.getTransaction().rollback();
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
}
